package com.icss.th.fulltext.lucenne;

import java.io.IOException;
import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;

import com.icss.th.common.util.StringUtil;
import com.icss.th.db.pojo.PageInfo;
import com.icss.th.fulltext.pojo.SearchResult;

public class LuceneHandler {

	/*
	 * 实现分页查询
	 */
	public static TopDocs search(IndexSearcher searcher, Query query, PageInfo pageInfo) {
		TopDocs result = null;
		try {
			ScoreDoc currentScoreDoc = null;
			int pageNum = pageInfo.getPageNum();
			int pageSize = pageInfo.getCountPerPage();
			int index = (pageNum-1) * pageSize;
			
			if(index > 0){
				TopDocs topDocs = searcher.search(query, 1000000);
				currentScoreDoc = topDocs.scoreDocs[index-1];
				result = searcher.searchAfter(currentScoreDoc, query, pageSize);
			}else{
				result = searcher.search(query, pageSize);
			}
			
			//设置总记录数、总页数
			pageInfo.setRecordCount(result.totalHits);
			if(result.totalHits > 0){
				pageInfo.setPageCount((result.totalHits / pageSize) + 1);
			}else{
				pageInfo.setPageCount(0);
			}
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}catch(ArrayIndexOutOfBoundsException e){
			e.printStackTrace();
		}
		
		return result;
	}

	
	/*
	 * 设置检索字段为高亮
	 */
	public static String higherIndex(String fieldName, String fieldValue,
			Query query, Analyzer analyzer) {
		String result = null;
		try {
			SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color='red'>", "</font>");
			Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));
			
			TokenStream tokenStream = analyzer.tokenStream(fieldName, new StringReader(fieldValue));
			result = highlighter.getBestFragment(tokenStream, fieldValue);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return result;
		
	}

	

	/*
	 * 封装Exam检索结果
	 */
	public static SearchResult settingExamResult(Document document, Query higherQuery, Analyzer analyzer) {
		SearchResult searchResult = new SearchResult();
		searchResult.setId(document.get("EXAMID"));
		
        String shortTitle = StringUtil.getLimitLengthString(document.get("EXAMTITLE"), 30, "...");
        String resultTmp1 = null;
        if(higherQuery != null){
        	resultTmp1 = LuceneHandler.higherIndex("EXAMTITLE", shortTitle, higherQuery, analyzer);
        }
        searchResult.setTitle(resultTmp1 == null ? shortTitle : resultTmp1);		//解决高亮返回null的
        
        String shortContent = StringUtil.getLimitLengthString(document.get("EXAMTITLE"), 140, "...");
        String resultTmp2 = null;
        if(higherQuery != null){
        	resultTmp2 = LuceneHandler.higherIndex("EXAMTITLE", shortContent, higherQuery, analyzer);
        }
        searchResult.setContent(resultTmp2 == null ? shortContent : resultTmp2);
        
        searchResult.setAnswer(document.get("EXAMANSWER"));
        searchResult.setSourceId(document.get("INPAPERID"));
        searchResult.setSourceName(document.get("PAPERTITLE"));
        
		return searchResult;
	}


	/*
	 * 封装Paper检索结果
	 */
	public static SearchResult settingPaperResult(Document document, Query higherQuery, Analyzer analyzer) {
		SearchResult searchResult = new SearchResult();
		searchResult.setId(document.get("PAPERID"));
        
        String shortTitle = StringUtil.getLimitLengthString(document.get("PAPERTITLE"), 30, "...");
        String resultTmp1 = LuceneHandler.higherIndex("PAPERTITLE", shortTitle, higherQuery, analyzer);
        searchResult.setTitle(resultTmp1 == null ? shortTitle : resultTmp1);		//解决高亮返回null的
		
        searchResult.setContent(LuceneHandler.higherIndex("PAPERTITLE", document.get("PAPERTITLE"), higherQuery, analyzer));
        searchResult.setSourceName(document.get("TYPENAME"));
        
		return searchResult;
	}

}
